#!/bin/sh

#SNMPV='2'
#TTZ='public1!'
#IP='134.85.128.101'

SNMPV=$1
TTZ=$2
IP=$3

# check Parma
if [ $# != 3 ]; then
  echo -e "\033[31mERROR\033[0m Should be e.g.: \033[32m$0 2 public1! 134.85.128.101\033[0m"
  exit 1; 
fi


function snmp_storage {
	#局部变量定义
	local result=""
	local err_msg=""
	local snmpvn=""

	function a {}

	if [ -n snmpwalk ]; then
		if [ $SNMPV == '1' ]; then
			snmpvn=""
			echo "暂不支持"
			exit 1
		elif [ $SNMPV == '2' ]; then
			snmpvn="snmpwalk -v 2c -c $TTZ $IP"
		elif [ $SNMPV == '3' ]; then
			snmpvn=""
			echo "暂不支持"
			exit 1
		else
			echo "only suppert 1,2,3"
		fi
	else
		echo "NO SNMPWALK"
	fi

	#名称
	function name {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			# .1.3.6.1.2.1.1.5.0
			$snmpvn sysName | awk -F':' '{print $NF}'
			return 0
		fi
	}

	#资源描述
	function resource_description {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			# .1.3.6.1.2.1.1.1.0
			$snmpvn sysDescr | awk -F':' '{print $NF}'
			return 0
		fi
	}
	#管理IP
	function manage_ip {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo $IP 
			return 0
		fi
	}
	#生产厂商[写死]
	function manufacturer {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo "huawei"
			return 0
		fi
	}
	#型号
	function manufacturer {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			$snmpvn '1.3.6.1.4.1.34774.4.1.1.2' | awk -F':' '{print $NF}'
			return 0
		fi
	}
	#序列号
	function serial_number {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			$snmpvn '1.3.6.1.4.1.34774.4.1.1.1' | awk -F':' '{print $NF}'
			return 0
		fi
	}
	#电源功率(W)[空]
	function power_supply {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#电源个数【空】
	function power_supply_number {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#前端口描述【空】
	function front_port {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#后端口描述【空】
	function back_port {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#Cache容量【MB】
	function cache_capacity {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			$snmpvn '1.3.6.1.4.1.34774.4.1.19.9.1.1.2' | awk -F':' '{print $NF}'
			return 0
		fi
	}
	#存储可用总容量【MB】
	function available_total_capacity {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			$snmpvn '1.3.6.1.4.1.34774.4.1.23.4.2.1.9' | awk -F':' '{print $NF}'
			return 0
		fi
	}
	#未分配容量【MB】
	function unused_capacity {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			$snmpvn '1.3.6.1.4.1.34774.4.1.23.4.2.1.10' | awk -F':' '{print $NF}'
			return 0
		fi
	}
	#已分配容量【MB】
	function used_capacity {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			$snmpvn '1.3.6.1.4.1.34774.4.1.23.4.2.1.8' | awk -F':' '{print $NF}'
			return 0
		fi
	}
	#RAID模式
	function raid_mode {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#RAID前总容量
	function raid_total_capacity {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#磁盘描述
	function desc {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			$snmpvn '1.3.6.1.4.1.34774.4.1.23.4.1.1.3' | awk -F':' '{print $NF}'
			return 0
		fi
	}
	#当前维保厂商
	function service_provider {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#原厂维保开始日期
	function service_start_date {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#原厂维保截止日期
	function service_due_date {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#第三方维保截止日期
	function otherservice_duedate {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#第三方维保开始日期
	function otherservice_startdate {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#购置合同号
	function contract {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
	#固定资产标签号
	function asset_tag_number {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
		#磁盘描述
	function desc {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
		#购置价格(元)
	function price {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
		#使用状态
	function use_state {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
		#起始U号
	function startu {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
		#结束U号
	function endu {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
		#机架位置号
	function rack_sn {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
		#位置编号
	function location_code {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
		#当前维保状态
	function maintain_status {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}
		#续保标识
	function xubao_state {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo ""
			return 0
		fi
	}

    #资产发现方式0-远程扫描，1-WEB爬虫，2-网络流量分析，3-日志分析，4-Agent，5-登录资产，6-手动导入，7-其他
    function found_typelist() {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			echo "Agent"
			return 0
		fi
    }
    #资产发现时间
    function found_type_time() {
        now_time=`date '+%Y%m%d%H%M%S'`
		if [ $? -ne 0 ];then
			err_msg="GET_NOW_TIME_FAILURE"
		fi
		if [ "$1" == 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			if [ "$now_time" ];then
				echo "$now_time"
				return 0
			else
				return 1
			fi
		fi
    }

	#资产版本号
	function information_version {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			$snmpvn '1.3.6.1.4.1.34774.4.1.1.6' | awk -F':' '{print $NF}'
			return 0
		fi
	}
	#CPU厂商
	function cpu_manufacturer {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
			$snmpvn '1.3.6.1.4.1.34774.4.1.19.8.12.1.15' | awk -F':' '{print $NF}'
			return 0
		fi
	}
    #是否上报,是、否、待上报
    function is_report() {
		if [ "$1" = 'err_msg' ];then
			echo "$err_msg"
			return 1
		else
				echo "待上报"
				return 0
		fi
    }
    #是否打印逗号
	function print_comma {
		if [ $1 -ne $2 ]; then
			echo ","
		fi
	}

	#打印结果
	function return_result {
		printf \
		'
		{	
			"messageCode": "success",
			"message": "",
			"data": [
		'
		host_names=($(serial_number))
		num=${#serial_number[@]}
		i=0
		#打印每个实例
			echo \
			'{
				"ciCode": "snmp_storage",
				"attributes": [
					{
					    "attrName": "名称",
					    "attrCode": "name",
					    "attrValue": "'`name`'",
					    "attrFailureReason": "'`name err_msg`'"
					},{
						"attrName": "资源描述",
						"attrCode": "resource_description",
						"attrValue": "'`resource_description`'",
						"attrFailureReason": "'`resource_description err_msg`'"		
					},{
						"attrName": "管理IP",
						"attrCode": "manage_ip",
						"attrValue": "'`manage_ip`'",
						"attrFailureReason": "'`manage_ip err_msg`'"		
					},{
						"attrName": "生产厂商",
						"attrCode": "manufacturer",
						"attrValue": "'`manufacturer`'",
						"attrFailureReason": "'`manufacturer err_msg`'"		
					},{
						"attrName": "型号",
						"attrCode": "model",
						"attrValue": "'`model`'",
						"attrFailureReason": "'`model err_msg`'"		
					},{
					    "attrName": "序列号",
					    "attrCode": "serial_number",
					    "attrValue": "'`serial_number`'",
					    "attrFailureReason": "'`serial_number err_msg`'"
					},{
					    "attrName": "电源功率(W)",
					    "attrCode": "power_supply",
					    "attrValue": "'`power_supply`'",
					    "attrFailureReason": "'`power_supply err_msg`'"
					},{
					    "attrName": "电源个数",
					    "attrCode": "power_supply_number",
					    "attrValue": "'`power_supply_number`'",
					    "attrFailureReason": "'`power_supply_number err_msg`'"
					},{
					    "attrName": "前端口描述",
					    "attrCode": "front_port",
					    "attrValue": "'`front_port`'",
					    "attrFailureReason": "'`front_port err_msg`'"
					},{
					    "attrName": "后端口描述",
					    "attrCode": "back_port",
					    "attrValue": "'`back_port`'",
					    "attrFailureReason": "'`back_port err_msg`'"
					},{
					    "attrName": "Cache容量",
					    "attrCode": "cache_capacity",
					    "attrValue": "'`cache_capacity`'",
					    "attrFailureReason": "'`cache_capacity err_msg`'"
					},{
					    "attrName": "存储可用总容量",
					    "attrCode": "available_total_capacity",
					    "attrValue": "'`available_total_capacity`'",
					    "attrFailureReason": "'`available_total_capacity err_msg`'"
					},{
					    "attrName": "未分配容量",
					    "attrCode": "unused_capacity",
					    "attrValue": "'`unused_capacity`'",
					    "attrFailureReason": "'`unused_capacity err_msg`'"
					},{
					    "attrName": "已分配容量",
					    "attrCode": "used_capacity",
					    "attrValue": "'`used_capacity`'",
					    "attrFailureReason": "'`used_capacity err_msg`'"
					},{
					    "attrName": "RAID模式",
					    "attrCode": "raid_mode",
					    "attrValue": "'`raid_mode`'",
					    "attrFailureReason": "'`raid_mode err_msg`'"
					},{
					    "attrName": "RAID前总容量",
					    "attrCode": "raid_total_capacity",
					    "attrValue": "'`raid_total_capacity`'",
					    "attrFailureReason": "'`raid_total_capacity err_msg`'"
					},{
					    "attrName": "磁盘描述",
					    "attrCode": "desc",
					    "attrValue": "'`desc`'",
					    "attrFailureReason": "'`desc err_msg`'"
					},{
					    "attrName": "当前维保厂商",
					    "attrCode": "service_provider",
					    "attrValue": "'`service_provider`'",
					    "attrFailureReason": "'`service_provider err_msg`'"
					},{
					    "attrName": "原厂维保开始日期",
					    "attrCode": "service_start_date",
					    "attrValue": "'`service_start_date`'",
					    "attrFailureReason": "'`service_start_date err_msg`'"
					},{
					    "attrName": "原厂维保截止日期",
					    "attrCode": "service_due_date",
					    "attrValue": "'`service_due_date`'",
					    "attrFailureReason": "'`service_due_date err_msg`'"
					},{
					    "attrName": "第三方维保截止日期",
					    "attrCode": "otherservice_duedate",
					    "attrValue": "'`otherservice_duedate`'",
					    "attrFailureReason": "'`otherservice_duedate err_msg`'"
					},{
					    "attrName": "第三方维保开始日期",
					    "attrCode": "otherservice_startdate",
					    "attrValue": "'`otherservice_startdate`'",
					    "attrFailureReason": "'`otherservice_startdate err_msg`'"
					},{
					    "attrName": "购置合同号",
					    "attrCode": "contract",
					    "attrValue": "'`contract`'",
					    "attrFailureReason": "'`contract err_msg`'"
					},{
					    "attrName": "固定资产标签号",
					    "attrCode": "asset_tag_number",
					    "attrValue": "'`asset_tag_number`'",
					    "attrFailureReason": "'`asset_tag_number err_msg`'"
					},{
					    "attrName": "购置价格(元)",
					    "attrCode": "price",
					    "attrValue": "'`price`'",
					    "attrFailureReason": "'`price err_msg`'"
					},{
					    "attrName": "使用状态",
					    "attrCode": "use_state",
					    "attrValue": "'`use_state`'",
					    "attrFailureReason": "'`use_state err_msg`'"
					},{
					    "attrName": "起始U号",
					    "attrCode": "startu",
					    "attrValue": "'`startu`'",
					    "attrFailureReason": "'`startu err_msg`'"
					},{
					    "attrName": "结束U号",
					    "attrCode": "endu",
					    "attrValue": "'`endu`'",
					    "attrFailureReason": "'`endu err_msg`'"
					},{
					    "attrName": "第三方维保开始日期",
					    "attrCode": "otherservice_startdate",
					    "attrValue": "'`otherservice_startdate`'",
					    "attrFailureReason": "'`otherservice_startdate err_msg`'"
					},{
					    "attrName": "机架位置号",
					    "attrCode": "rack_sn",
					    "attrValue": "'`rack_sn`'",
					    "attrFailureReason": "'`rack_sn err_msg`'"
					},{
					    "attrName": "位置编号",
					    "attrCode": "location_code",
					    "attrValue": "'`location_code`'",
					    "attrFailureReason": "'`location_code err_msg`'"
					},{
					    "attrName": "当前维保状态",
					    "attrCode": "maintain_status",
					    "attrValue": "'`maintain_status`'",
					    "attrFailureReason": "'`maintain_status err_msg`'"
					},{
					    "attrName": "续保标识",
					    "attrCode": "xubao_state",
					    "attrValue": "'`xubao_state`'",
					    "attrFailureReason": "'`xubao_state err_msg`'"
					},{
					    "attrName": "第三方维保开始日期",
					    "attrCode": "otherservice_startdate",
					    "attrValue": "'`otherservice_startdate`'",
					    "attrFailureReason": "'`otherservice_startdate err_msg`'"
					},{
						"attrName": "资产发现方式",
						"attrCode": "found_typelist",
						"attrValue": "'`found_typelist`'",
						"attrFailureReason": "'`found_typelist err_msg`'"		
					},{
						"attrName": "资产版本号",
						"attrCode": "information_version",
						"attrValue": "'`information_version`'",
						"attrFailureReason": "'`information_version err_msg`'"		
					},{
						"attrName": "CPU信息",
						"attrCode": "cpu_manufacturer",
						"attrValue": "'`cpu_manufacturer`'",
						"attrFailureReason": "'`cpu_manufacturer err_msg`'"		
					},{
						"attrName": "资产发现时间",
						"attrCode": "found_type_time",
						"attrValue": "'`found_type_time`'",
						"attrFailureReason": "'`found_type_time err_msg`'"		
					},{
						"attrName": "是否上报",
						"attrCode": "is_report",
						"attrValue": "'`is_report`'",
						"attrFailureReason": "'`is_report err_msg`'"		
					}
				],
				"relations": [ 
					{
						"quoteCiCode": "minicomputer",
						"relationName": "安装于",
						"quoteInstances": [
							[
								{
									"attrName": "序列号",
									"attrCode": "serial_number",
									"attrValue": "'`get_serial_number`'",
									"attrFailureReason": "'`get_serial_number err_msg`'"
								}
							]
						]
					}
				]
			}'`print_comma $i $num`''
		echo "]}"
	}
	return_result;
}


#错误定义
function return_error {
	case $1 in 
	'UNKNOW_OS')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "unknow os type",
				"data": []
			}
		'
		;;
	'NO_INSTANCE')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "no database",
				"data": []
			}
		'
		;;
	'CMD_EXECUTE_ERROR')
		printf \
		'
			{	
				"messageCode": "error",
				"message": "ps execute error",
				"data": []
			}
		'
		;;
	esac
}

#自发现入口
function main {
	os_type=`uname -s`
	case ${os_type} in
		'Linux')
				snmp_storage
			;;
		'*')
			return_error 'UNKNOW_OS';
			;;
	esac
}

main;